<template>
  <div class="login-container">
    <el-form
      ref="loginFormRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">智能移动机器人（中山）研究院</h3>
        <h4 class="title">后台监控系统</h4>
      </div>

      <el-form-item prop="username">
        <SvgIcon class="m-2 ml-3 w-4 h-4" fill="#889aa4" name="user"> </SvgIcon>
        <el-input
          v-model="loginForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <SvgIcon class="m-2 ml-3 w-4 h-4" fill="#889aa4" name="password">
        </SvgIcon>
        <el-input
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
        />
        <span class="show-pwd" @click="showPwd">
          <SvgIcon
            v-if="passwordType === 'password'"
            class="m-2 ml-3 w-4 h-4"
            fill="#889aa4"
            name="eye"
          ></SvgIcon>
          <SvgIcon
            v-else
            class="m-2 ml-3 w-4 h-4"
            fill="#889aa4"
            name="eye-open"
          ></SvgIcon>
        </span>
      </el-form-item>

      <el-checkbox
        v-model="ifRememberPassword"
        class="mb-4 text-white"
        label="记住密码"
        @change="handleRememberPassword"
      ></el-checkbox>

      <el-button
        id="login-button"
        :loading="loading"
        type="primary"
        @click.prevent="handleLogin"
      >
        Login
      </el-button>
    </el-form>
  </div>
</template>

<script lang="ts">
export default {
  name: "LoginPage",
};
</script>
<script setup lang="ts">
import { reactive, ref } from "vue";
import { setCookie, getCookie } from "@/utils/support.js";
import { useUserStore } from "@/store/user.js";
import { useRouter } from "vue-router";
import { useTagsViewStore } from "@/store/tagsView.js";
import { useRememberPassword } from "@/composables/useRememberPassword";
const { delAllViews } = useTagsViewStore();
const { ifRememberPassword, handleRememberPassword, getRememberPassword } =
  useRememberPassword();

const loginForm = reactive({
  username: "",
  password: "",
});
const loginRules = {
  username: [{ required: true, trigger: "blur", message: "用户名不能为空" }],
  password: [{ required: true, trigger: "blur", message: "密码不能为空" }],
};

const passwordType = ref("password");
function showPwd() {
  passwordType.value === "password"
    ? (passwordType.value = "")
    : (passwordType.value = "password");
}

loginForm.username = getCookie("username") || "";
loginForm.password = getRememberPassword();

const loading = ref(false);
const loginFormRef = ref(null);
const userStore = useUserStore();
const router = useRouter();
function handleLogin() {
  loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      const { login } = userStore;
      try {
        await login(loginForm);
        loading.value = false;
        setCookie("username", loginForm.username, 15);
        if (ifRememberPassword.value) {
          setCookie("password", loginForm.password, 15);
        }
        delAllViews();
        router.push("/");
      } catch {
        loading.value = false;
      }
    } else {
      return false;
    }
  });
}
</script>

<style lang="scss" scoped>
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

.login-container {
  :deep(.el-input) {
    display: inline-block;
    width: 85%;
    .el-input__wrapper {
      width: 100%;
      background: transparent;
      box-shadow: none;
      .el-input__inner {
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  #login-button {
    width: 100%;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    width: 35px;
    display: inline-flex;
    align-items: center;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
